//
//  XYTableViewCell.m
//  项目二cell
//
//  Created by rimi on 15/12/11.
//  Copyright © 2015年 xiaoyi. All rights reserved.
//

#import "XYTableViewCell.h"

typedef NS_ENUM (NSInteger, XYCELLStatus) {
    XYCellTimeStatus = 0,
    XYCellListStatus ,
    XYCellOkStatus
};

@interface XYTableViewCell() <UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIPanGestureRecognizer *panRecognizer;
@property (nonatomic, assign) CGPoint panStartPoint;

@property (nonatomic, assign) BOOL isback;
@property (nonatomic, assign) CGFloat lastMovePoint;

@property (nonatomic, assign) CGFloat newMovePoint;
@property (nonatomic, assign) BOOL isNewPointSet;
@end
@implementation XYTableViewCell

- (void)awakeFromNib {
    self.panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panThisCell:)];

    self.isback = NO;
    self.lastMovePoint = 0.0f;
    [self.contentView addGestureRecognizer:self.panRecognizer];
    self.panRecognizer.delegate = self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (NSString *)transFormStringToDateString:(NSString *)string {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss +zzz"];
    NSDate *dateString = [formatter dateFromString:string];
    [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
    return [formatter stringFromDate:dateString];
}

- (void)setCellpattern:(NSArray *)array {
    self.NameLabel.text = [NSString stringWithFormat:@"%@", array[0]];
    if (self.cellStatus == XYCellTimeStatus) {
        self.ThePointImage.image = [UIImage imageNamed:@"iconfont-naozhong"];
        self.SetTime.text = [NSString stringWithFormat:@"%@", [self transFormStringToDateString:array[6]]];
        [self transFormStringToDateString:array[6]];
        self.SetTime.textColor = [UIColor redColor];
        self.LeftImageView.image = [UIImage imageNamed:@"back_list"];
        self.RightImageView.image = [UIImage imageNamed:@"naochong"];
    } else if (self.cellStatus == XYCellOkStatus) {
        self.ThePointImage.image = [UIImage imageNamed:@"iconfont-finish"];
        self.SetTime.text = [NSString stringWithFormat:@"%@", array[6]];
        self.SetTime.textColor = [UIColor colorWithRed:0.067 green:0.788 blue:0.341 alpha:1.000];
        self.RightImageView.image = [UIImage imageNamed:@"back_list"];
    } else if (self.cellStatus == XYCellListStatus) {
        self.ThePointImage.image = [UIImage imageNamed:@"iconfont-ttpodicon"];
        self.SetTime.text = array[1];
        self.SetTime.textColor = [UIColor colorWithRed:0.941 green:0.741 blue:0.000 alpha:1.000];
        self.LeftImageView.image = [UIImage imageNamed:@"sure"];
        self.RightImageView.image = [UIImage imageNamed:@"naochong"];
    }
    
    NSString *repeat = [NSString stringWithFormat:@"%@", array[2]];
    self.PushRepeat = repeat;
    self.cancelPushTime = array[6];
    if ((![repeat isEqualToString:@"从不"]) || !(repeat.length == 0)) {
        self.Repeat.image = [UIImage imageNamed:@"repeatHighLight"];
    } else {
        self.Repeat.image = [UIImage imageNamed:@"repeat"];
    }
    if ([array[3] length] != 0) {
        self.Tag.image = [UIImage imageNamed:@"tagHighLight"];
    } else {
        self.Tag.image = [UIImage imageNamed:@"tag"];
    }
    if ([array[4] length] != 0) {
        self.Note.image = [UIImage imageNamed:@"noteHighLight"];
    } else {
        self.Note.image = [UIImage imageNamed:@"note"];
    }
}

#pragma mark - cell左右滑删除逻辑
- (void)panThisCell:(UIPanGestureRecognizer *)recognizer
{
    //    NSLog(@"%ld", recognizer.state);//1为开始，2为正在移动，3为结束
    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan:
            self.panStartPoint = [recognizer translationInView:self.myContentView];
            break;
            
        case UIGestureRecognizerStateChanged: {
            CGPoint currentPoint = [recognizer translationInView:self.myContentView];
            CGFloat deltaX = currentPoint.x - self.panStartPoint.x;

            if (self.cellStatus == XYCellOkStatus) {
                if (deltaX > 20) {
                    if (deltaX <= self.newMovePoint) {
                        self.myContentRightConstraint.constant = self.newMovePoint - deltaX - 20;
                        self.myContentLeftConstraint.constant = -self.myContentRightConstraint.constant;
                    } else {
                        //最大限定
                        self.newMovePoint = deltaX;
                        deltaX = 20;
                        self.myContentRightConstraint.constant = -deltaX;
                        self.myContentLeftConstraint.constant = -self.myContentRightConstraint.constant;
                        deltaX = self.newMovePoint;
                    }
                } else {
                    self.myContentRightConstraint.constant = self.newMovePoint - deltaX - 20;
                    self.myContentLeftConstraint.constant = -self.myContentRightConstraint.constant;
                }
            } else {
                self.myContentRightConstraint.constant = -deltaX;
                self.myContentLeftConstraint.constant = -self.myContentRightConstraint.constant;
            }
            
            if (self.myContentLeftConstraint.constant < 0) {
                //向左移
                if (self.lastMovePoint >= self.myContentLeftConstraint.constant) {
                    //保留最大距离
                    self.lastMovePoint = self.myContentLeftConstraint.constant;
                    self.isback = NO;
                    self.LeftImageView.alpha = 0.0f;
                } else {
                    //发生返回操作
                    self.isback = YES;
                    self.lastMovePoint = self.myContentLeftConstraint.constant;
                    self.LeftImageView.alpha = 1.0f;
                }
            } else {
                //向右移
                if (self.lastMovePoint <= self.myContentLeftConstraint.constant) {
                    self.lastMovePoint = self.myContentLeftConstraint.constant;
                    self.isback = NO;
                    self.RightImageView.alpha = 0.0f;
                } else {
                    self.isback = YES;
                    self.lastMovePoint = self.myContentLeftConstraint.constant;
                    self.RightImageView.alpha = 1.0f;
                }
            }
            
            //动态改变透明度
            if (fabs(self.myContentRightConstraint.constant) > 375/3 && self.isback == NO) {
                self.belowView.alpha = 1;
            } else {
                self.belowView.alpha = 0.3;
            }
            //动态改变颜色
            self.belowView.backgroundColor = [self getTheBelowBackColor:self.myContentRightConstraint.constant status:self.cellStatus];
        }
            break;
            
        case UIGestureRecognizerStateEnded:{
            self.newMovePoint = 0.0f;
            //超过3分之一才能消除
            if (self.myContentRightConstraint.constant >= 375/3 && self.isback == NO) {
                //向右补满
                [self setConstraintsFromRightView:YES];
            } else if (self.myContentLeftConstraint.constant >= 375/3 && self.isback == NO) {
                //向左铺满
                [self setConstraintsFromLeftView:YES];
            } else {
                //缩回去
                [self resetConstraintContstantsToZero:YES];
                self.isback = NO;
                self.lastMovePoint = 0.0f;
            }
        }
            break;
        default:
            break;
    }
}

#pragma mark - 自定义方法
- (UIColor *)getTheBelowBackColor:(CGFloat)right status:(NSInteger)status {
    if ((status == XYCellListStatus && right > 0) || (status == XYCellTimeStatus && right > 0)) {
        return [UIColor colorWithRed:1.000 green:0.400 blue:0.400 alpha:1.000];
    } else if (status == XYCellListStatus && right < 0) {
        return [UIColor colorWithRed:0.067 green:0.788 blue:0.341 alpha:1.000];
    } else if (status == XYCellOkStatus && right < 0) {
        return [UIColor whiteColor];
    }
    return [UIColor colorWithRed:0.941 green:0.741 blue:0.000 alpha:1.000];
}

//动画
- (void)updateConstraints:(void (^)(BOOL finished))completion {
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [self layoutIfNeeded];
    } completion:completion];
}

//回到之前位置
- (void)resetConstraintContstantsToZero:(BOOL)finished {
    [self updateConstraints:^(BOOL finished) {
        self.myContentRightConstraint.constant = 0;
        self.myContentLeftConstraint.constant = 0;
        [self updateConstraints:^(BOOL finished) {
            self.LeftImageView.alpha = 1.0f;
            self.RightImageView.alpha = 1.0f;
        }];
    }];
}

//从右向左删除
- (void)setConstraintsFromRightView:(BOOL)finished {
    [self updateConstraints:^(BOOL finished) {
        self.myContentLeftConstraint.constant = -[self cellWidth];
        self.myContentRightConstraint.constant = [self cellWidth];
        
        [self updateConstraints:^(BOOL finished) {
            //补满后才通知代理
            if (self.cellStatus == XYCellOkStatus) {
                [self.delegate deleteTheChoosedCellToLeftOK:self];
            } else {
                [self.delegate deleteTheChoosedCellToLeft:self];
            }
        }];
    }];
}

//从左向右删除
- (void)setConstraintsFromLeftView:(BOOL)animated {
//    [self.delegate deleteTheChoosedCell:self];
    
    [self updateConstraints:^(BOOL finished) {
        self.myContentLeftConstraint.constant = [self cellWidth];
        self.myContentRightConstraint.constant = -[self cellWidth];
        
        [self updateConstraints:^(BOOL finished) {
            [self.delegate deleteTheChoosedCellToRight:self];
        }];
    }];
}

- (CGFloat)cellWidth {
    return CGRectGetWidth(self.contentView.frame);
}

/*重用机制*/
- (void)prepareForReuse {
    [super prepareForReuse];
    self.myContentLeftConstraint.constant = 0;
    self.myContentRightConstraint.constant = 0;
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        CGPoint translation = [(UIPanGestureRecognizer *)gestureRecognizer translationInView:self];
        return fabs(translation.x) > fabs(translation.y);
    }
    return YES;
}

//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
//
//    return YES;
//}
//
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
//    return YES;
//}
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
//    return YES;
//}
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//    return YES;
//}

@end
